// 封装的工具类 // 引入crypto-js
import * as CryptoJS from 'crypto-js';

const key = CryptoJS.enc.Utf8.parse('2022@gkpt2050!@@'); //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse('2022@gkpt2050!@@'); //十六位十六进制数作为密钥偏移量

// 导出加密方法
export function Encrypt(word) {
  const src = CryptoJS.enc.Utf8.parse(word);
  const encrypted = CryptoJS.AES.encrypt(src, key, {iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
  return encrypted.ciphertext.toString().toUpperCase();
}

// utf8编码明文和密钥转uft8
export function utf8Parse(str) {
  if (!str) return
  return CryptoJS.enc.Utf8.parse(str)
}

// 导出AES解密的方法
export function handleAESDecrypt(encryptStr) {
  let key = "fTlJFFvlUaBUrFkZ"
  let encryptedHexStr = CryptoJS.enc.Hex.parse(encryptStr)//指定HEx
  let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
  let decrypted = CryptoJS.AES.decrypt(srcs, utf8Parse(key), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  let currentContent = decrypted.toString(CryptoJS.enc.Utf8);
  if (!currentContent) currentContent = encryptStr;
  if (currentContent == encryptStr) currentContent = '';
  // console.log(currentContent, '解码后的数据')
  return currentContent;
}

